<template>
  <div class="layoutlist">
    <el-button type="primary" size="default" @click="dialogFormVisible = true"
       :disabled="!qxobj.add">新建品类</el-button
    >
    <el-table :data="tableData" border stripe style="margin-top: 10px">
      <el-table-column
        v-for="col in columns"
        :prop="col.id"
        :key="col.id"
        :label="col.label"
        align="center"
      >
      </el-table-column>
      <el-table-column label="操作" align="center">
        <template slot-scope="{ row }">
          <el-button type="text" size="default" @click="onEdit(row)"  :disabled="!qxobj.save"
            >编辑</el-button
          >
          <el-button
            type="text"
            size="default"
            style="color: red"
            @click="onDelete(row.id)"
             :disabled="!qxobj.del"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <pagination />
    <el-dialog
      :title="form.id?'编辑品类':'新建品类'"
      :visible.sync="dialogFormVisible"
      :close-on-click-modal="false"
      width="400px"
      @close="closeDialog"
    >
      <el-form
        :model="form"
        :rules="rules"
        ref="form"
        label-position="left"
        label-width="100px"
      >
        <el-form-item label="品类名称" prop="title">
          <el-input v-model="form.title"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            size="default"
            @click="onCreation"
            :loading="loading"
            >{{form.id?"修改":"新建"}}</el-button
          >
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import {
  postApigetcategory,
  postApieditcategory,
  postApidelcategory,
} from "@/api";
import pagination from "@/components/pagination.vue";
export default {
  components: {
    pagination,
  },
  data() {
    return {
      qxobj:{},
      dialogFormVisible: false,
      loading: false,
      form: {
        title: "",
      },
      rules: {
        title: [{ required: true, message: "请输入品类名称", trigger: "blur" }],
      },
      tableData: [],
      columns: [
        {
          label: "品类名称",
          id: "title",
        },
        {
          label: "创建时间",
          id: "createtime",
        },
      ],
    };
  },
  activated() {
    this.qxobj = JSON.parse(this.$route.query.qxobj)
    this.postApigetcategory();
  },
  methods: {
    // 品类列表
    postApigetcategory() {
      postApigetcategory().then((res) => {
        console.log("品类列表", res);
        this.tableData = res.data;
      });
    },
    closeDialog() {
      this.form = { title: "" };
    },
    // 删除
    onDelete(id) {
      this.$confirm("此操作将永久删除该品类, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          postApidelcategory(id)
            .then((res) => {
              this.$message.success("删除成功");
              this.postApigetcategory();
            })
            .catch((err) => {
              this.$message.warning(err.msg);
            });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    // 编辑
    onEdit(e) {
      this.form = {
        title: e.title,
        id: e.id,
      };
      this.dialogFormVisible = true;
    },
    // 创建编辑品类
    onCreation() {
      this.loading = true;
      this.$refs["form"].validate((valid) => {
        if (valid) {
          this.loading = false;
          postApieditcategory(this.form)
            .then((res) => {
              console.log("编辑品类", res);
              if (this.form.id) {
                this.$message.success("编辑品类成功");
              } else {
                this.$message.success("新建品类成功");
              }
              this.postApigetcategory();
              this.loading = false;
              this.dialogFormVisible = false;
              this.form = { title: "" };
            })
            .catch((err) => {
              console.log("err", err);
              this.$message.warning(err.msg);
              this.loading = false;
            });
        } else {
          this.loading = false;
          return false;
        }
      });
    },
  },
};
</script>

<style lang="scss" scoped>
.layoutlist {
  @include global-back;
}
</style>